<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Upload</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f0f4f8;
            margin: 0;
            padding: 0;
        }
        .container {
            max-width: 600px;
            margin: 40px auto;
            padding: 20px;
            background: #ffffff;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #333;
            margin-bottom: 20px;
            font-size: 2em;
            text-align: center;
        }
        form {
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .form-group {
            width: 100%;
            margin-bottom: 20px;
        }
        input[type="file"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: #f9f9f9;
            box-sizing: border-box;
            font-size: 1em;
        }
        input[type="submit"] {
            background-color: #007bff;
            color: #ffffff;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            font-size: 1em;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        input[type="submit"]:hover {
            background-color: #0056b3;
        }
        #progress-container {
            width: 100%;
            background-color: #f3f3f3;
            border-radius: 4px;
            margin-bottom: 20px;
            display: none;
        }
        #progress-bar {
            width: 0;
            height: 20px;
            background-color: #007bff;
            border-radius: 4px;
        }
        #progress-text {
            text-align: center;
            margin-top: 10px;
            font-size: 1em;
        }
    </style>
</head>
<body>

    <div class="container">
        <h1>Upload Your File</h1>
        <form id="upload-form">
            <div class="form-group">
                <input type="file" name="file" id="file-input" required>
            </div>
            <div id="progress-container">
                <div id="progress-bar"></div>
            </div>
            <div id="progress-text"></div>
            <div class="form-group">
                <input type="submit" value="Upload">
            </div>
        </form>
    </div>

    <script>
        document.getElementById('upload-form').addEventListener('submit', function(event) {
            event.preventDefault(); // Prevent the default form submission

            const fileInput = document.getElementById('file-input');
            const file = fileInput.files[0];

            if (!file) {
                alert('Please select a file!');
                return;
            }

            const formData = new FormData();
            formData.append('file', file);

            const xhr = new XMLHttpRequest();
            xhr.open('POST', '/upload', true);

            // Display progress bar
            document.getElementById('progress-container').style.display = 'block';

            // Track progress
            xhr.upload.onprogress = function(event) {
                if (event.lengthComputable) {
                    const percentComplete = (event.loaded / event.total) * 100;
                    document.getElementById('progress-bar').style.width = percentComplete + '%';
                    document.getElementById('progress-text').innerText = `Upload Progress: ${Math.round(percentComplete)}%`;
                }
            };

            // Handle upload completion
            xhr.onload = function() {
                if (xhr.status === 200) {
                    document.getElementById('progress-text').innerText = 'Upload Complete!';
                } else {
                    document.getElementById('progress-text').innerText = 'Upload Failed. Please try again.';
                }
            };

            // Handle error
            xhr.onerror = function() {
                document.getElementById('progress-text').innerText = 'Upload Error. Please try again.';
            };

            // Send the form data
            xhr.send(formData);
        });
    </script>

</body>
</html>
